package com.trsvax.gwthello.client.gwtui.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.Gauge;
import com.google.gwt.visualization.client.visualizations.Gauge.Options;
import com.trsvax.gwthello.client.gwtui.services.GaugesService;
import com.trsvax.gwthello.client.gwtui.services.GaugesServiceAsync;

public class GaugeWidget extends Composite {
	Gauge widget;
	private final GaugesServiceAsync service = GWT.create(GaugesService.class);

	public GaugeWidget(Element element, Dictionary dictionary) {
		widget = new Gauge();
		initWidget(widget);
		// service.getOptions("");
		Timer t = new Timer() {
			public void run() {
				loadData();
			}
		};
		t.scheduleRepeating(2000);
	}

	void loadData() {
		Integer i = Random.nextInt(12);
		widget.draw(data(i), options());
	}

	Options options() {
		Options options = Options.create();
		options.setWidth(400);
		options.setHeight(240);
		options.setGaugeRange(0, 24);
		options.setGreenRange(0, 6);
		options.setYellowRange(6, 12);
		options.setRedRange(12, 24);

		return options;
	}

	public DataTable data(Integer work) {
		DataTable data = DataTable.create();
		data.addColumn(ColumnType.STRING, "Task");
		data.addColumn(ColumnType.NUMBER, "Hours per Day");
		data.addRows(5);
		data.setValue(0, 0, "Work");
		data.setValue(0, 1, work);
		data.setValue(1, 0, "Eat");
		data.setValue(1, 1, 2);
		data.setValue(2, 0, "Commute");
		data.setValue(2, 1, 2);
		data.setValue(3, 0, "Watch TV");
		data.setValue(3, 1, 2);
		data.setValue(4, 0, "Sleep");
		data.setValue(4, 1, 7);
		return data;

	}

}
